<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.controller');

class OwnserviceControllerLevel extends JController
{
	/**
	 * Controller construct
	*/
	function __construct()
	{
		parent::__construct();

		//Register task
		$this->registerTask('add', 'edit');
	}

	/**
	* Method to add a level
	*
	* @access	public
	* @since	1.0
	*/
	function edit() 
	{
		global $mainframe;

		// Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();

		// Check permission access
		if($user->gid < 25) 
		{
			if (!Ownservice::checkAccess( 'vip', 'manage' )) 
			{
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
		}

		// Create the view
		$view = & $this->getView('level', 'html');

		// Get/Create the model
		$model = & $this->getModel('Level');

		// Push the model into the view (as default)
		$view->setModel($model, true);

		// Set the layout
		$view->setLayout('form');

		// Display the view
		$view->display();
	}

	/**
	 * Logic to save level
	 * 
	 * @access public
	 * @return void
	*/
	function save()
	{
		//Check for request forgeries
		JRequest::checkToken() or jexit('Invalid Token');

		// Get vars
		$post = JRequest::get( 'post' );

		$model = $this->getModel('Level');

		if($model->store($post)){
			$type = 'message';
			$msg = JText::_('OWNSERVICE_LEVEL_SAVED');
		} else {
			$type = 'error';
			$msg = JText::_('OWNSERVICE_ERROR_SAVING_LEVEL');
		}

		$link = JRoute::_('index.php?option=com_ownservice&view=levels');
		
		$this->setRedirect($link, $msg, $type);
	}

	/**
	 * Logic to delete level
	 *
	 * @access public
	 * @return void
	 * @since 1.0
	*/
	function delete()
	{
		global $mainframe;

		// Check for request forgeries
		JRequest::checkToken('request') or jexit( 'Invalid Token' );

		$user	= & JFactory::getUser();
		$app	= & JFactory::getApplication();

		// Check permission access
		if($user->gid < 25) 
		{
			if (!Ownservice::checkAccess( 'vip', 'manage' )) 
			{
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
		}

		//get var
		$id = JRequest::getVar('id',0, 'post', 'int');

		$model=& $this->getModel('Levels');
		if ($model->delete($id)) 
		{
			$type = 'message';
			$msg = JText::_('OWNSERVICE_LEVEL_DELETED');
		} else 
		{
			$type = 'error';
			$msg = JText::_( 'OWNSERVICE_LEVEL_FAILED' );
		}

		$link = JRoute::_('index.php?option=com_ownservice&view=levels');

		$this->setRedirect( $link, $msg, $type );
	}

	/**
	 * logic for cancel an action
	 *
	 * @access public
	 * @return void
	 * @since 1.0
	 */
	function cancel()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$link = JRoute::_('index.php?option=com_ownservice&view=levels');

		$this->setRedirect( $link );
	}
}